package bTree.LeetCode;

import java.util.Arrays;

public class 包含所有1的最小矩形面积1 {
    public int minimumArea(int[][] grid) {
        int top = 0, bottom = 0, left = 0, right = 0;
        int row = grid.length;
        int line = grid[0].length;
        top = findTop(grid,row,line);
        bottom=findBottom(grid,row,line);
        left=findleft(grid,row,line);
        right=findright(grid,row,line);
        return (right-left+1)*(bottom-top+1);

    }

    private int findright(int[][] grid, int row, int line) {
        for (int i = line-1; i >=0; i--) {
            for (int i1 = 0; i1 < row; i1++) {
                if (grid[i1][i] == 1) {
                    return i;
                }
            }
        }
        return 0;
    }

    private int findleft(int[][] grid, int row, int line) {
        for (int i = 0; i < line; i++) {
            for (int i1 = 0; i1 < row; i1++) {
                if (grid[i1][i] == 1) {
                    return i;
                }
            }
        }
        return 0;
    }

    private int findBottom(int[][] grid, int row, int line) {
        for (int i = row-1; i >=0; i--) {
            for (int i1 = 0; i1 < line; i1++) {
                if (grid[i][i1] == 1) {
                    return i;
                }
            }
        }
        return 0;
    }


    private int findTop(int[][] grid,int row,int line) {
        for (int i = 0; i < row; i++) {
            for (int i1 = 0; i1 < line; i1++) {
                if (grid[i][i1] == 1) {
                    return i;
                }
            }
        }
        return 0;
    }


}